package cn.maidouya.pet.service.impl;

import cn.maidouya.basic.service.impl.BaseServiceImpl;
import cn.maidouya.basic.util.*;
import cn.maidouya.order.domain.PetAcquisitionOrder;
import cn.maidouya.order.mapper.PetAcquisitionOrderMapper;
import cn.maidouya.org.domain.Employee;
import cn.maidouya.org.domain.Shop;
import cn.maidouya.org.mapper.EmployeeMapper;
import cn.maidouya.org.mapper.ShopMapper;
import cn.maidouya.pet.domain.Pet;
import cn.maidouya.pet.domain.PetDetail;
import cn.maidouya.pet.domain.PetType;
import cn.maidouya.pet.domain.SearchMasterMsg;
import cn.maidouya.pet.mapper.PetDetailMapper;
import cn.maidouya.pet.mapper.PetMapper;
import cn.maidouya.pet.mapper.PetTypeMapper;
import cn.maidouya.pet.mapper.SearchMasterMsgMapper;
import cn.maidouya.pet.query.SearchMasterMsgQuery;
import cn.maidouya.pet.service.ISearchMasterMsgService;
import cn.maidouya.user.domain.User;
import cn.maidouya.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;
    @Override
    public void publish(SearchMasterMsg searchMasterMsg, Long logininfoId) {
        // 1. 寻找最近店铺
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        // 2.计算最近店铺
        List<Shop> shops = shopMapper.loadAll();
        Shop nearestShop = DistanceUtil.getNearestShop(point, shops);
        // 3.绑定最近店铺
        searchMasterMsg.setShop_id(nearestShop.getId());
        // 4.绑定用户信息
        User user = userMapper.loadByLogininfoId(logininfoId);
        searchMasterMsg.setUser_id(user.getId());
        // 5.存储寻主消息
        searchMasterMsgMapper.save(searchMasterMsg);
        // 6.给店铺老板发个消息
        Employee employee = employeeMapper.loadById(logininfoId);
        // 给老板发短信
        //SmsUtil.send(employee.getPhone(), "老板您好，来【"+searchMasterMsg.getTitle()+"】活了，赶快去接客!");
        System.out.println("老板您好，来【"+searchMasterMsg.getTitle()+"】活了，赶快去接客!");

    }
    // 待处理消息 pending
    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery searchMasterMsgQuery, Long productId) {
        // 1. 设置消息状态 0 待处理  1已处理
        searchMasterMsgQuery.setState(0);
        // 2. 校验是否店铺管理员消息
        Employee employee = employeeMapper.loadByLogininfoId(productId);
        if (employee.getShop_id() != null){
            searchMasterMsgQuery.setShopId(employee.getShop_id());
        }
        // 3. 平台消息(业务交给父类处理)
        return super.queryData(searchMasterMsgQuery);
    }
    // 已处理消息 processed
    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery searchMasterMsgQuery, Long productId) {
        // 1. 设置消息状态 0 待处理  1已处理
        searchMasterMsgQuery.setState(1);
        // 2. 校验员工是否店铺管理员
        Employee employee = employeeMapper.loadByLogininfoId(productId);
        if (employee.getShop_id() != null){
            searchMasterMsgQuery.setShopId(employee.getShop_id());
        }
        // 3. 平台查询所有交给父类处理
        return super.queryData(searchMasterMsgQuery);
    }
    // 用户自己消息
    @Override
    public PageList<SearchMasterMsg> userList(SearchMasterMsgQuery searchMasterMsgQuery, Long productId) {
        Employee employee = employeeMapper.loadByLogininfoId(productId);
        searchMasterMsgQuery.setUserId(employee.getId());
        return super.queryData(searchMasterMsgQuery);
    }

    @Override
    public void handle(Pet pet,Long logininfoId) {
        // 1. 跳转信息状态 state状态：0下架 1上架
        searchMasterMsgMapper.updateState2Proceed(pet.getSearch_master_msg_id());
        // 2. 添加pet宠物对象信息
        petMapper.save(pet);
        PetDetail detail = pet.getDetail();
        if (detail != null){
            detail.setPet_id(pet.getId());
            petDetailMapper.save(detail);
        }
        // 生成订单@TODO
        // 查询登录人员工
        Employee employee = employeeMapper.loadByLogininfoId(logininfoId);
        // 查询寻主人信息
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(pet.getSearch_master_msg_id());
        // 调用方法生成订单编号
        PetAcquisitionOrder petAcquisitionOrder = pet2order(pet, searchMasterMsg, employee.getId());
        // 执行添加订单(订单快照)
        petAcquisitionOrderMapper.save(petAcquisitionOrder);

        // 生成订单支付@TODO
        System.out.println("支付成功：" + pet.getCostprice());


    }
        private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt,Long empId) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        order.setState(0);//待支付
        order.setPrice(pet.getCostprice());
        order.setAddress(adopt.getAddress());
        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(adopt.getUser_id());
        order.setPaytype(0);
        order.setShop_id(pet.getShop_id());
        order.setEmp_id(empId);
        return order;
    }
}